package com.foxzzz.temporary;

public class Solution1071 {

    public String gcdOfStrings(String str1, String str2) {
        String maxStr = str1.length() > str2.length() ? str1 : str2;
        String minStr = str1.length() > str2.length() ? str2 : str1;

        String temp = null;
        //灵感来自辗转相除法
        while (temp == null || !temp.equals("")) {
            temp=maxStr;
            while (temp.startsWith(minStr)&&!minStr.equals("")) {
                temp = temp.replaceFirst(minStr, "");
            }
            maxStr = minStr;
            minStr = temp;

            if (!maxStr.startsWith(minStr))
                return "";
        }
        return temp == null ? "" : maxStr;
    }
    public static void main(String[] args) {
        System.out.println(new Solution1071().gcdOfStrings("ABCABC", "ABC"));

    }

}
